package com.app.micaihu.listener;


import android.graphics.drawable.Animatable;
import android.view.ViewGroup;

import com.app.micaihu.custom.view.CustomImageView;
import com.app.utils.util.LogUtils;
import com.app.utils.util.ViewSizeUtils;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.image.ImageInfo;

/**
 * Created by Guo on 2016/9/13.
 * <p>
 * <p>
 * 根据给定的高，来自动适应宽的 fresco 的监听
 */
public class AutoWidthControllerListener<INFO> extends BaseControllerListener<INFO> {

    private CustomImageView mCustomImageView;
    private OnLoadCompeleted onLoadCompeleted;
    private boolean isLongPic;

    public AutoWidthControllerListener(CustomImageView mCustomImageView) {
        this.mCustomImageView = mCustomImageView;
    }

    @Override
    public void onSubmit(String id, Object callerContext) {
        super.onSubmit(id, callerContext);
        mCustomImageView.setAspectRatio(1);
    }


    @Override
    public void onFinalImageSet(String id, INFO imageInfo, Animatable animatable) {
        super.onFinalImageSet(id, imageInfo, animatable);

        if (imageInfo == null) {
            return;
        }

        if (imageInfo instanceof ImageInfo) {
            ImageInfo imageInfo1 = (ImageInfo) imageInfo;


            int height = imageInfo1.getHeight();
            int width = imageInfo1.getWidth();

            float rate = (float) width / (float) height;


            if (rate < 0.1f) {//宽/高 小于0.1,不足十分之一,则确定是长图
                rate = 0.5f;
                isLongPic = true;
            }
            mCustomImageView.setMaxWidth(ViewSizeUtils.getWidthPixels());
            float aspectRatio = mCustomImageView.getAspectRatio();

            if (aspectRatio != rate) {
                mCustomImageView.setAspectRatio(rate);
            }


            if(onLoadCompeleted != null){
                onLoadCompeleted.onLoadCompeleted(mCustomImageView,isLongPic);
            }
//                }
//            }
//
//
//            LogUtils.e("gzq", "layout___hou______height = "+layoutParams.height+"   width = "+layoutParams.width);

        }

//        mCustomImageView.getHierarchy().setPlaceholderImage(R.drawable.loadpic_big_empty_listpage, ScalingUtils.ScaleType.CENTER_CROP);


    }



    public interface OnLoadCompeleted{
        void onLoadCompeleted(CustomImageView civ,boolean flag);
    }



    public void setOnLoadCompeleted(OnLoadCompeleted onLoadCompeleted){

        this.onLoadCompeleted = onLoadCompeleted;
    }


}
